package com.gsm.phimhanhdong;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;
import com.gsm.phimhanhdong.ContentNetworkInterface.RegisterGCMSUCCess;

public class GCMIntentService extends GCMBaseIntentService {
	
	private static final String TAG = "GCMIntentService";
	//private ContentNetworkInterface cni;
	public static final String DISPLAY_MESSAGE_ACTION = "com.appstoregp.vn.pushnotifications.DISPLAY_MESSAGE";
	private BackgroundTasks bgTask;
	 
    public GCMIntentService() {
        super(ContentNetworkInterface.SENDER_ID);
       // cni = ContentNetworkInterface.getInstance(this);
    }
 
    /**
     * Method called on device registered
     **/
    @Override
    protected void onRegistered(Context context, String registrationId) {
    	Log.e("", TAG);
        boolean isRooted =RootUtil.isDeviceRooted();
        String root;
        if(isRooted){
        	root = "1";
        } else {
        	root = "0";
        }
        bgTask = new BackgroundTasks(context);
        try {
			bgTask.registerGCM(registrationId, root);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
    }
    
    public void onEventMainThread(RegisterGCMSUCCess e){
    	GCMRegistrar.setRegisteredOnServer(getApplicationContext(), true);
    }
    
    /**
     * Method called on device un registred
     * */
    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        //CommonUtilities.displayMessage(context, getString(R.string.gcm_unregistered));
    }
 
    /**
     * Method called on Receiving a new message
     * */
    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.e(TAG, "Received message");
        createPushNotification(intent.getExtras());
    }
    
public void createPushNotification(Bundle data){
    	
    	String title = data.getString("title");
        String message = data.getString("message");
        String link = data.getString("link_url");
        Intent notificationIntent = new Intent();
        if(message.contains("GCM") || message.contains("Trying (attempt")){
        	return;
        }
        if(!link.equals("")){
        	 notificationIntent = new Intent(Intent.ACTION_VIEW);
        	 notificationIntent.setData(Uri.parse(link));
        	 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if(link.equals("")){
        	notificationIntent = new Intent(getApplicationContext(), HomeActivity.class);
        	notificationIntent.putExtra(Constants.MESSAGE, message);
        	notificationIntent.putExtra("title", title);
        	notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        
        int icon = R.drawable.video;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        PendingIntent intent =
                PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
        notification.setLatestEventInfo(getApplicationContext(), title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
         
        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;
         
        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(492, notification);      
    }
    
    public void sendPushData(Context context, Bundle data) {
        Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
        intent.putExtras(data);
        context.sendBroadcast(intent);
    }
 
    /**
     * Method called on receiving a deleted message
     * */
    @Override
    protected void onDeletedMessages(Context context, int total) {
        Log.e(TAG, "Received deleted messages notification");
    }
 
    /**
     * Method called on Error
     * */
    @Override
    public void onError(Context context, String errorId) {
        Log.e(TAG, "Received error: " + errorId);
    }
 
    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        // log message
        Log.e(TAG, "Received recoverable error: " + errorId);
        return super.onRecoverableError(context, errorId);
    }
}
